package com.senthink.gl.business.forgetpw;

import com.senthink.gl.business.common.exception.BusinessException;
import com.senthink.gl.business.common.exception.DataAccessException;
import com.senthink.gl.business.common.util.RandomUtils;
import com.senthink.gl.business.domain.entity.User;
import com.senthink.gl.business.domain.service.ForgetPasswordDataService;
import com.senthink.gl.business.domain.service.UserService;
import com.senthink.gl.business.domain.service.VerifyCodeDataService;
import com.senthink.gl.business.messages.CodeMsg;
import com.senthink.gl.business.response.ApiResponse;
import com.senthink.gl.business.response.CodeMsgDataResponse;
import com.senthink.gl.business.userlog.SystemUserLog;
import com.senthink.gl.business.userlog.enums.LogDetailEnum;
import com.senthink.gl.business.userlog.enums.LogOperateEnum;
import com.senthink.gl.business.userlog.enums.LogTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 重置密码Service
 *
 * @author yuanpeng@senthink.com
 * @data 2018-01-24 15:28
 */

@Service
public class ForgetPasswordService {

    @Autowired
    private ForgetPasswordDataService dataService;

    @Autowired
    private UserService userService;
    @Autowired
    private VerifyCodeDataService verifyCodeDataService;

    @Autowired
    private CodeMsg codeMsg;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 一天之内允许忘记密码次数
     */
    @Value("${forgetpw.count}")
    private int maxCount;

    private final int EXPIRED_MILLIS = 3 * 60 * 1000;

    private final int RESET_MILLIS = 50 * 1000;

    Logger LOGGER = LoggerFactory.getLogger(this.getClass());


    /**
     * 重置密码，校验是否能重置密码
     *
     * @param email  邮箱
     * @param mobile 手机
     * @return
     */
    public ApiResponse forgetPw(String email, String mobile, String verifyCode) {

        return new CodeMsgDataResponse<>(codeMsg.successCode(), codeMsg.successMsg());
    }

    /**
     * 请求图形校验码
     *
     * @param request
     * @param response
     * @param email
     * @param mobile
     * @return
     */
    @Transactional(rollbackFor = DataAccessException.class)
    public ApiResponse verifyCode(HttpServletRequest request, HttpServletResponse response, String email, String mobile) {

        return new CodeMsgDataResponse(codeMsg.successCode(), codeMsg.successMsg());
    }

    /**
     * 校验验证码
     *
     * @param email
     * @param mobile
     * @param code
     * @return
     */
    public ApiResponse checkCode(String email, String mobile, String code) {

        return new CodeMsgDataResponse(codeMsg.failureCode(), codeMsg.failureMsg());
    }

    /**
     * 重置密码
     *
     * @param request
     * @return
     */
    @SystemUserLog(operate = LogOperateEnum.UPDATE, detail = LogDetailEnum.FORGET_PASSWORD, logType = LogTypeEnum.USER)
    @Transactional(rollbackFor = DataAccessException.class)
    public ApiResponse resetPw(ForgetPasswordRequest request) {

        return new CodeMsgDataResponse(codeMsg.failureCode(), codeMsg.failureMsg());
    }

    /**
     * 校验用户是否存在以及获取验证码次数
     *
     * @param email
     * @param mobile
     * @return
     */
    public ApiResponse checkAccount(String email, String mobile) {


        return new CodeMsgDataResponse(codeMsg.successCode(), codeMsg.successMsg());

    }

    /**
     * 校验是否有重置密码资格
     *
     * @param account
     */
    private void checkAccountCode(String account) {

    }

    /**
     * 发送邮件
     *
     * @param user
     * @return
     */
    private void sendEmail(User user) {

    }

    /**
     * 发送短信
     *
     * @param user
     */
    private void sendSms(User user) {

    }

    /**
     * 校验验证码是否正确
     *
     * @param account
     * @param code
     * @return
     */
    @Transactional(rollbackFor = DataAccessException.class)
    public boolean checkCodeExpired(String account, String code) {

            throw new BusinessException(codeMsg.smsNoCodeCode(), codeMsg.smsNoCodeMsg());
    }

    /**
     * 校验验证码是否正确
     *
     * @param account
     * @param code
     * @return
     */
    @Transactional(rollbackFor = DataAccessException.class)
    public void checkCode(String account, String code) {

            throw new BusinessException(codeMsg.smsNoCodeCode(), codeMsg.smsNoCodeMsg());
    }

    /**
     * 校验上次请求验证码时间
     *
     * @param user
     * @return
     */
    @Transactional(rollbackFor = DataAccessException.class)
    public boolean checkCodeTime(User user) {

            return true;
    }

    /**
     * 校验图形验证码合法性
     *
     * @param user
     * @param code
     */
    private void checkVerifyCode(User user, String code) {

        throw new BusinessException(codeMsg.verCodeExpiredCode(), codeMsg.verCodeExpiredMsg());
    }

}
